import axios from "axios";
import QS from 'qs';


const service = axios.create({
	timeout: 5000,
	withCredentials: true,
});



// request拦截器
axios.interceptors.request.use(
	config => {
		console.log("config.js-->>加载token")
		let token = localStorage.getItem("token");
		if (token != '') {
			//携带请求头
			config.headers.Authorization = localStorage.getItem("token")
		}
		return config;
	},
	error => {
		console.log(error);
	}
);

// 响应拦截器
axios.interceptors.response.use(
	response => {
		//获取服务器返回的状态码
		let code = response.data.code;
		if (code === 500) {
			this.$message.error('操作失败,请稍后重试');
		} else if (code >= 2001 && code <= 2004) {
			alert('需要先登录哦~')
			// 清除token
			// localStorage.removeItem('token');
			// 清除消息提示
			localStorage.removeItem('chatMessageStr');
			localStorage.removeItem('privateLetterStr');
			localStorage.removeItem('systemMessageStr');
			//跳转到登录页面
			window.location.href = "/#/login";
		}
		return response;
	},
	error => {
		if (error.response.status == 500) {
			this.$message.error("哦豁~服务器走丢了")
		} 
		return response;
	}

);


export default service;
